<template>
  <div class="menu-create">
    <el-card class="box-card">
      <template
        #header
        class="clearfix"
      >
        <span>{{ isEdit ? '编辑菜单' : '添加菜单' }}</span>
      </template>
      <el-form
        ref="formDom"
        :model="form"
        label-width="80px"
      >
        <el-form-item label="菜单名称">
          <el-input v-model="form.name" />
        </el-form-item>
        <el-form-item label="菜单路径">
          <el-input v-model="form.href" />
        </el-form-item>
        <el-form-item label="上级菜单">
          <el-select
            v-model="form.parentId"
            placeholder="请选择上级菜单"
          >
            <el-option
              :key="-1"
              label="无上级菜单"
              :value="-1"
            />
            <el-option
              v-for="item in parentMenuList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="描述">
          <el-input v-model="form.description" />
        </el-form-item>
        <el-form-item label="前端图标">
          <el-input v-model="form.icon" />
        </el-form-item>
        <el-form-item label="是否显示">
          <el-radio-group v-model="form.shown">
            <el-radio :label="true">
              是
            </el-radio>
            <el-radio :label="false">
              否
            </el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="排序">
          <el-input-number
            v-model="form.orderNum"
            :min="1"
            label="描述文字"
          />
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            @click="onSubmit(form)"
          >
            提交
          </el-button>
          <el-button
            v-if="!isEdit"
          >
            重置
          </el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script lang="ts">
import { useRouter, useRoute } from 'vue-router'
import { defineComponent, onBeforeMount, Ref, ref } from 'vue'
import { createOrUpdateMenu, getEditMenuInfo, getMenuInfo } from '/@/services/menu.ts'
import { ElMessage } from 'element-plus'

interface Form {
  name: string
  href: string
  parentId: number | null
  description: string
  icon: string
  shown: boolean
  orderNum: number
  id?: number
}
let form: Ref<Form>
// 提交
const useAddOrEditMenu = () => {
  const router = useRouter()
  const onSubmit = async (form: Form) => {
    const { data } = await createOrUpdateMenu(form)
    if (data.code === '000000') {
      ElMessage.success('添加菜单成功!')
      router.push('/menu')
    }
  }
  return {
    onSubmit
  }
}
// 初始化数据
const uesInit = () => {
  const route = useRoute()
  const routeId = +route.params?.id
  const isEdit = route.name === 'menuEdit'
  const parentMenuList = ref([{ id: 1, name: '无选项' }])
  const getParentMenuList = async () => {
    try {
      const { data } = await getEditMenuInfo()
      if (data.code === '000000') {
        parentMenuList.value = data?.data?.parentMenuList || []
      }
    } catch (error) {
      ElMessage.error('获取上级菜单失败!')
    }
  }
  onBeforeMount(async () => {
    // 获取上级菜单
    getParentMenuList()
    if (isEdit) {
      const { data } = await getMenuInfo(routeId)
      form.value = data.data
    }
  })
  return {
    routeId,
    isEdit,
    parentMenuList
  }
}
export default defineComponent({
  name: 'MenuIndex',
  setup () {
    form = ref<Form>({ name: '', href: '', parentId: null, description: '', icon: '', shown: true, orderNum: 1 })
    return {
      ...uesInit(),
      ...useAddOrEditMenu(),
      form,
      formDom: null
    }
  }
})
</script>

<style lang="scss" scoped>
  .box-card {
    :deep(.el-card) {
      height: calc(100% - 60px - 45px);
    }
  }
</style>
